@extends('admin.layouts.app')

@section('title', '系统设置 - 挪车小助手后台管理')

@section('content')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">系统设置</h1>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                微信小程序配置
            </div>
            <div class="card-body">
                <form action="{{ route('admin.settings.update') }}" method="POST" class="row g-3">
                    @csrf
                    <div class="col-md-6">
                        <label for="app_name" class="form-label">应用名称</label>
                        <input type="text" class="form-control" id="app_name" name="app_name" value="{{ $settings['app_name'] }}">
                        <div class="form-text">显示在小程序页面和通知中的应用名称</div>
                    </div>
                    
                    <div class="col-md-6">
                        <label for="app_url" class="form-label">应用URL</label>
                        <input type="text" class="form-control" id="app_url" name="app_url" value="{{ $settings['app_url'] }}">
                        <div class="form-text">应用服务器的URL地址，例如: https://example.com</div>
                    </div>
                    
                    <div class="col-md-6">
                        <label for="wechat_app_id" class="form-label">微信AppID</label>
                        <input type="text" class="form-control" id="wechat_app_id" name="wechat_app_id" value="{{ $settings['wechat_app_id'] }}">
                        <div class="form-text">微信小程序的AppID，在微信公众平台获取</div>
                    </div>
                    
                    <div class="col-md-6">
                        <label for="wechat_app_secret" class="form-label">微信AppSecret</label>
                        <input type="text" class="form-control" id="wechat_app_secret" name="wechat_app_secret" value="{{ $settings['wechat_app_secret'] }}">
                        <div class="form-text">微信小程序的AppSecret，在微信公众平台获取</div>
                    </div>
                    
                    <div class="col-md-12">
                        <div class="alert alert-info">
                            <i class="bi bi-info-circle"></i> 注意: 修改微信小程序配置后，需要重启应用服务器才能生效。
                        </div>
                    </div>
                    
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">保存设置</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row mt-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                管理员账号管理
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <h5>当前管理员</h5>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>姓名</th>
                                    <th>最后登录</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($admins as $admin)
                                <tr>
                                    <td>{{ $admin->id }}</td>
                                    <td>{{ $admin->username }}</td>
                                    <td>{{ $admin->name }}</td>
                                    <td>{{ $admin->last_login_at ?? '未登录' }}</td>
                                    <td>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <button type="button" class="btn btn-warning" 
                                                data-bs-toggle="modal" 
                                                data-bs-target="#editAdminModal" 
                                                data-id="{{ $admin->id }}"
                                                data-username="{{ $admin->username }}"
                                                data-name="{{ $admin->name }}">
                                                <i class="bi bi-pencil"></i>
                                            </button>
                                            @if(count($admins) > 1 && $admin->id != session('admin_id'))
                                            <button type="button" class="btn btn-danger" 
                                                data-bs-toggle="modal" 
                                                data-bs-target="#deleteAdminModal" 
                                                data-id="{{ $admin->id }}"
                                                data-name="{{ $admin->name }}">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                            @endif
                                        </div>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
                
                <div class="mt-3">
                    <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addAdminModal">
                        <i class="bi bi-plus-circle"></i> 添加管理员
                    </button>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                系统信息
            </div>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        PHP版本
                        <span>{{ $system_info['php_version'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        Laravel版本
                        <span>{{ $system_info['laravel_version'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        数据库类型
                        <span>{{ $system_info['database_type'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        服务器操作系统
                        <span>{{ $system_info['os'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        Web服务器
                        <span>{{ $system_info['web_server'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        用户数量
                        <span>{{ $system_info['users_count'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        车主数量
                        <span>{{ $system_info['car_owners_count'] }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        扫码总次数
                        <span>{{ $system_info['scans_count'] }}</span>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- 添加管理员弹窗 -->
<div class="modal fade" id="addAdminModal" tabindex="-1" aria-labelledby="addAdminModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addAdminModalLabel">添加管理员</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="/admin/admins" method="POST">
                @csrf
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="add_username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="add_username" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="add_name" class="form-label">姓名</label>
                        <input type="text" class="form-control" id="add_name" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="add_password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="add_password" name="password" required>
                    </div>
                    <div class="mb-3">
                        <label for="add_password_confirmation" class="form-label">确认密码</label>
                        <input type="password" class="form-control" id="add_password_confirmation" name="password_confirmation" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-success">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑管理员弹窗 -->
<div class="modal fade" id="editAdminModal" tabindex="-1" aria-labelledby="editAdminModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editAdminModalLabel">编辑管理员</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editAdminForm" action="" method="POST">
                @csrf
                @method('PUT')
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="edit_username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="edit_username" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_name" class="form-label">姓名</label>
                        <input type="text" class="form-control" id="edit_name" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_password" class="form-label">新密码 (留空则不修改)</label>
                        <input type="password" class="form-control" id="edit_password" name="password">
                    </div>
                    <div class="mb-3">
                        <label for="edit_password_confirmation" class="form-label">确认新密码</label>
                        <input type="password" class="form-control" id="edit_password_confirmation" name="password_confirmation">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 删除管理员弹窗 -->
<div class="modal fade" id="deleteAdminModal" tabindex="-1" aria-labelledby="deleteAdminModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteAdminModalLabel">删除管理员</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除管理员 <strong id="deleteAdminName"></strong> 吗？此操作不可恢复。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteAdminForm" action="" method="POST">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 编辑管理员弹窗处理
        const editAdminModal = document.getElementById('editAdminModal');
        editAdminModal.addEventListener('show.bs.modal', function (event) {
            const button = event.relatedTarget;
            const adminId = button.getAttribute('data-id');
            const username = button.getAttribute('data-username');
            const name = button.getAttribute('data-name');
            
            const editAdminForm = document.getElementById('editAdminForm');
            const editUsername = document.getElementById('edit_username');
            const editName = document.getElementById('edit_name');
            
            editAdminForm.action = "/admin/admins/" + adminId;
            editUsername.value = username;
            editName.value = name;
        });
        
        // 删除管理员弹窗处理
        const deleteAdminModal = document.getElementById('deleteAdminModal');
        deleteAdminModal.addEventListener('show.bs.modal', function (event) {
            const button = event.relatedTarget;
            const adminId = button.getAttribute('data-id');
            const adminName = button.getAttribute('data-name');
            
            const deleteAdminForm = document.getElementById('deleteAdminForm');
            const deleteAdminNameElem = document.getElementById('deleteAdminName');
            
            deleteAdminForm.action = "/admin/admins/" + adminId;
            deleteAdminNameElem.textContent = adminName;
        });
    });
</script>
@endsection 